﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Barry.BarbeSystem.Business;
using Barry.BarbeSystem.Filters;
using Barry.BarbeSystem.Models;

namespace Barry.BarbeSystem.Controllers
{
    [CustomerAuthorizationFilter]
    public class BarberDetailController : Controller
    {
        private IRepository<BarberDetail> _detailRepository;

        private IRepository<BarberCategory> _categoryRepository;

        private BarberDetailRepository _barberDetailRepository;

        public BarberDetailController()
        {
            _detailRepository = new Repository<BarberDetail>();
            _categoryRepository = new Repository<BarberCategory>();
            _barberDetailRepository = new BarberDetailRepository();
        }

        public ActionResult Index()
        {
            var list = _detailRepository.GetAll();


            return View(list);
        }


        public JsonResult PageIndex(BarberDetailPageRequest request)
        {
            //return View();
            int itemCounts;
            var results = _barberDetailRepository.Get(request, out itemCounts);

            JsonResult jes = new JsonResult();
            jes.Data = new GridResponseInfo() { rows = results, total = itemCounts };
            return jes;
        }


        //
        // GET: /BarberDetail/Create

        public ActionResult Create()
        {

            var categories = _categoryRepository.GetAll().Where(i => i.Enable == true);;

            ViewData["category"] = categories.Select(c => new SelectListItem()
                {
                    Text = c.Name,
                    Value = c.Id.ToString()
                });

            return View();
        }

        //
        // POST: /BarberDetail/Create

        [HttpPost]
        public ActionResult Create(BarberDetail model)
        {
            try
            {
                string categoryId = Request["category"];

                model.BarberCategory = new BarberCategory() { Id = Int32.Parse(categoryId) };

                _detailRepository.Update(model);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /BarberDetail/Edit/5

        public ActionResult Edit(int id)
        {
            var model = _detailRepository.Get(id);
            if (model == null)
            {
                return HttpNotFound();
            }

            InitCategory(model);

            return View(model);
        }

        private void InitCategory(BarberDetail model)
        {
            var categories = _categoryRepository.GetAll().Where(i => i.Enable == true);
            var result = categories.Select(c => new SelectListItem()
                {
                    Text = c.Name,
                    Value = c.Id.ToString()
                }).ToList();
            result.ForEach(delegate(SelectListItem item)
                {
                    if (model.BarberCategory.Id.ToString() == item.Value)
                    {
                        item.Selected = true;
                    }
                });

            ViewData["Category"] = result;
        }

        //
        // POST: /BarberDetail/Edit/5

        [HttpPost]
        public ActionResult Edit(BarberDetail model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    string categoryId = Request["category"];

                    model.BarberCategory = new BarberCategory() { Id = Int32.Parse(categoryId) };

                    _detailRepository.Update(model);


                    return RedirectToAction("Index");
                }
            }
            catch
            {
            }


            model = _detailRepository.Get(model.Id);
            InitCategory(model);
            return View(model);
        }

        //
        // GET: /BarberDetail/Delete/5

       [HttpPost]
        public JsonResult GetDetailByCategory(int id)
        {
            JsonResult j= new JsonResult();
           var details =from item in  _barberDetailRepository.GetAll()
                        where item.BarberCategory.Id ==id
                        select new BarberDetail(){Id = item.Id, Name =item.Name, Money = item.Money};
           j.Data = details;
           return j;
        }
        


        [HttpPost]
        public ActionResult Delete(int id)
        {
            JsonResult j = new JsonResult();
            var obj = _barberDetailRepository.Get(id);
            if (obj != null)
            {

                _barberDetailRepository.Delete(obj);
                j.Data = new { success = true };

            }
            else
            {
                j.Data = new { success = false };
            }
            return j;
        }
    }
}
